// Workaround for duplicators all of this is required for constraints to not sag
local ids =1

//Assign the constraint and Identity and record entities information so that...
//We can reposition it later while it's  being constrained
local function NextStep( Constraint )

	Constraint.Identity=ids
	ids=ids+1

		if(Constraint.Ent!=nil and ValidEntity(Constraint.Ent))then
			Constraint.Ent1Ang = Constraint.Ent:GetAngles()
		end

		if(Constraint.Ent1!=nil and ValidEntity(Constraint.Ent1))then
			Constraint.Ent1Ang = Constraint.Ent1:GetAngles()
			
				if(Constraint.Ent2!=nil and ValidEntity(Constraint.Ent2) )then
					Constraint.EntityPos = Constraint.Ent1:GetPos() - Constraint.Ent2:GetPos()
					Constraint.Ent2Ang = Constraint.Ent2:GetAngles()
				end
				
				if(Constraint.Ent4!=nil and ValidEntity(Constraint.Ent4) )then
					Constraint.EntityPos = Constraint.Ent1:GetPos() - Constraint.Ent4:GetPos()
					Constraint.Ent4Ang = Constraint.Ent4:GetAngles()
				end

		end

end

local function FixMagnet(Magnet)
	Magnet.Entity = Magnet
end

//Find out when a Constraint is created
hook.Add( "OnEntityCreated", "weld_identities", function(Constraint)

	if not ValidEntity( Constraint ) then return end
	
	local class = string.Explode("_", Constraint:GetClass())
	if class[2] == "magnet" then
		timer.Simple( 0, FixMagnet, Constraint )
	end
	
	if class[1] == "phys" then
		timer.Simple( 0, NextStep, Constraint )
	end

end )